package com.apes.fa.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaInventoryProcessTable.CONST_TABLE_NAME)
public class FaInventoryProcessTable extends Model {

    public static final String CONST_TABLE_NAME = "fa_inventory_process_table";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "资产盘点处理表", prefix = "PZ%(company)", padding = 5)
    @Comment("单号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @Column(name = "fa_whereabouts")
    @Comment("资产去向")
    private String faWhereabouts;

    @Column(name = "business_name")
    @Comment("单据名称")
    private String businessName;

    @Column(name = "business_state")
    @Comment("单据状态")
    private String businessState;

    @Column(name = "inventory_id")
    @Comment("盘点编号")
    private String inventoryId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @Column(name = "total_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("盘亏金额合计")
    private double totalAmount = 0;

    @OneToMany(mappedBy = "faInventoryProcessTable", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<FaInventoryLossCard> faInventoryLossCards = new ArrayList<>();

    public List<FaInventoryLossCard> getFaInventoryLossCards() {
        for (FaInventoryLossCard faInventoryLossCard : faInventoryLossCards) {
            faInventoryLossCard.setFaInventoryProcessTable(this);
        }
        return faInventoryLossCards;
    }

}
